package com.boliq.lol;


import android.content.Intent;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.boliq.demo.R;
import com.boliq.lol.adapter.MenuAdapter;
import com.boliq.lol.base.BaseActivity;
import com.boliq.lol.bean.MenuBean;
import com.boliq.lol.ui.news.MyFragment;
import com.boliq.lol.ui.news.NewsMainFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;



public class LolMainActivity extends BaseActivity {

    private DrawerLayout mDrawerLayout;//侧滑菜单
    private ImageView userHeadView;//玩家头像
    private TextView userNameView;//玩家姓名
    private TextView levelView;//玩家等级
    private RecyclerView menuListView;//菜单列表
    private MenuAdapter mMenuAdapter;//菜单列表适配器
    private View rootContentView;//左拉隐藏菜单栏
    private View drawerBtn;//左上角点击按钮

    private View scanView;//点击扫描按钮
    private View moreView;//点击更多按钮
    private View settingBtn;//点击设置按钮
    private BottomNavigationView mBottomNavigationView;//顶部导航栏
    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();
    private Fragment fragment;
    @Override
    protected int contentView() {
        return R.layout.activity_lol;
    }

    @Override
    protected void initView() {
        mDrawerLayout = findViewById(R.id.drawer_layout);
        drawerBtn = findViewById(R.id.img_drawer_btn);
        userHeadView = findViewById(R.id.img_user_head);
        userNameView = findViewById(R.id.tv_user_nick);
        levelView = findViewById(R.id.tv_user_level);
        menuListView = findViewById(R.id.rv_menu_list);
        rootContentView = findViewById(R.id.view_main_content);
        mBottomNavigationView = findViewById(R.id.bottom_nav_view);

        scanView = findViewById(R.id.img_scan_btn);
        moreView = findViewById(R.id.img_more_btn);
        settingBtn = findViewById(R.id.ll_setting_btn);

    }

    @Override
    protected void initData() {
        menuListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mMenuAdapter = new MenuAdapter();
        menuListView.setAdapter(mMenuAdapter);
    }

    @Override
    protected void bindData() {
        super.bindData();
        userHeadView.setImageResource(R.drawable.profile_data);
        userNameView.setText("zsd");
        levelView.setText("Lv1");

        List<MenuBean> menuBeans = new ArrayList<>();
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_message_24, "我的评论"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_mail_24, "与我相关"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_assignment_24, "我的订单"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_check_box_24, "我的任务"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_loyalty_24, "我的收藏"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_card_giftcard_24, "活动中心"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_verified_user_24, "信用分"));
        mMenuAdapter.addData(menuBeans);


        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment = getContent(item);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fl_main_content, fragment)
                    .commit();
            return true;
        });
        mBottomNavigationView.setSelectedItemId(R.id.tab_news);
    }
    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);
        }
        return fragment;
    }

    private Fragment nf(MenuItem menuItem) {

//       fragment.setArguments(NewsMainFragment.newArgument(menuItem.getTitle().toString()));
        switch (menuItem.getItemId()){
            case R.id.tab_news:
                fragment=new NewsMainFragment();
                break;
            case R.id.tab_user_center:
                fragment=new MyFragment();
                break;
            default:
                fragment=new NewsMainFragment();
        }
        return fragment;
    }



    @Override
    protected void bindEvent() {
        //左上角按钮点击事件
        drawerBtn.setOnClickListener(v -> mDrawerLayout.openDrawer(GravityCompat.START));
        //点击扫描按钮点击事件
        scanView.setOnClickListener(v -> Toast.makeText(LolMainActivity.this, "点击扫描", Toast.LENGTH_SHORT).show());
        //点击更多按钮点击事件
        moreView.setOnClickListener(v -> Toast.makeText(LolMainActivity.this, "点击更多", Toast.LENGTH_SHORT).show());
        //设置按钮点击事件
        settingBtn.setOnClickListener(v ->  startActivity(new Intent(LolMainActivity.this, LolSettingActivity.class)));
        //左拉菜单栏点击事件
        mMenuAdapter.setOnItemClickListener(new MenuAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(View view, int position) {
                switch (position) {
                    case 0:
                        Toast.makeText(LolMainActivity.this, "你点击了 我的评论", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(LolMainActivity.this, "你点击了 与我相关", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(LolMainActivity.this, "你点击了 我的订单", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(LolMainActivity.this, "你点击了 我的任务", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(LolMainActivity.this, "你点击了 我的收藏", Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        Toast.makeText(LolMainActivity.this, "你点击了 活动中心", Toast.LENGTH_SHORT).show();
                        break;
                    case 6:
                        Toast.makeText(LolMainActivity.this, "你点击了 信用分", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
